define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    table: 'log',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                pk:"id",
                sortName: 'updatetime',
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                 queryParams:function(params) {
                    // 要显示的字段
                    params.fields = '';
                    // 要关联的拓展信息
                    params.extra = 'member,partner,live,open_auth,open_server';
                    return params;
                },
                columns: [
                    [
                        {
                            field: 'member.nickname'
                            ,title: __('Member')
                            ,align: 'left'
                            ,operate:'LIKE %...%'
                            ,selectPage:{
                                'source': '/member/member.admin/selectpage',
                                'search-table': 'member/admin',
                                'select-to-close-list': false,
                                'show-field':"nickname"
                            }
                            ,name:"mid"
                            ,placeholder:"请选择"
                        },
                        {field: 'action', title: __('Action'), formatter: Controller.api.formatter.action_text,searchList: {'scan': __('Scan'),'issued': __('Issued'),'cancel': __('Cancel')},
                            style: 'min-width:100px;'},
                        {field: 'own_type', title: __('OwnType'), formatter: Controller.api.formatter.own_type,searchList: {'partner': __('Partner'),'live': __('Live'),'admin': __('Admin')},
                            style: 'min-width:100px;'},
                        {
                            field: 'live.name',
                            title: __('OwnLive'),
                            align:'left',
                            operate:"ET",
                            selectPage:{
                                'source': '/live/admin.live/selectpage',
                                'search-table': 'live/live',
                                'select-to-close-list': false
                            },
                            name:"live_id",
                            placeholder:"请选择"
                        },
                        {
                            field: 'partner.name'
                            ,title: __('Partner')
                            ,align:'left'
                            ,operate:'ET'
                            ,selectPage:{
                                'source': '/partner/admin.partner/selectpage',
                                'search-table': 'partner/partner',
                                'select-to-closeList': false,
                            }
                            ,name:"own_partner_id"
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'open_server.name'
                            ,title: __('Server')
                            ,align:'left'
                            ,operate:"IN(...)"
                            ,selectPage:{
                                'source': '/wechat/admin.server/selectpage',
                                'search-table': 'wechat/open_server',
                                'select-to-close-list': false,
                                'show-field':"name",
                                'key-field':"user_name",
                                'multiple':true
                            }
                            ,name:'user_name'
                            ,placeholder:"请选择"
                        },
                        {
                            field: 'open_auth.name'
                            ,title: __('Auth')
                            ,align:'left'
                            ,operate:"ET"
                            ,selectPage:{
                                'source': '/wechat/admin.auth/selectpage',
                                'search-table': 'wechat/open_auth',
                                'select-to-closeList': false,
                                'key-field':'user_name'
                            }
                            ,name:"sid"
                            ,placeholder:"请选择"
                        },
                        {field: 'createtime', title: __("Create time"),sortable:true,operate:false,formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __("Update time"),sortable:true,operate:false,formatter: Table.api.formatter.datetime,visible: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));

            },
            formatter: {
                own_type: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.own_type == 'partner') {
                        _color = 'success';
                    }
                    return '<span class="text-'+ _color +'"> ' + row.own_type_text + '</span>';
                },
                action_text: function (value, row, index, custom) {
                    //渲染状态
                    var _color = 'primary';
                    if (row.action == 'cancel') {
                        _color = 'danger';
                    }else if(row.action == 'focus'){
                        _color = 'success';
                    }
                    return '<span class="text-'+ _color +'"> ' + row.action_text + '</span>';
                }
            }
        },
    };
    return Controller;
});